﻿// Decompiled with JetBrains decompiler
// Type: KKtLib.VAG
// Assembly: PD_Tool_Private, Version=0.4.5.3, Culture=neutral, PublicKeyToken=null
// MVID: 8A8DE4DE-5B2D-492A-9C2E-FC536B2E689B
// Assembly location: F:\divax\data\PCSB01007\rom\PD_Tool_Private.exe

using KKtLib.Types;
using System;
using System.Collections.Generic;
using System.IO;

namespace KKtLib
{
  public class VAG
  {
    public static readonly short[,] HEVAG_coefs = new short[128, 4]
    {
      {
        (short) 0,
        (short) 0,
        (short) 0,
        (short) 0
      },
      {
        (short) 7680,
        (short) 0,
        (short) 0,
        (short) 0
      },
      {
        (short) 14720,
        (short) -6656,
        (short) 0,
        (short) 0
      },
      {
        (short) 12544,
        (short) -7040,
        (short) 0,
        (short) 0
      },
      {
        (short) 15616,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) 14731,
        (short) -7059,
        (short) 0,
        (short) 0
      },
      {
        (short) 14507,
        (short) -7366,
        (short) 0,
        (short) 0
      },
      {
        (short) 13920,
        (short) -7522,
        (short) 0,
        (short) 0
      },
      {
        (short) 13133,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) 12028,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) 10764,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) 9359,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) 7832,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) 6201,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) 4488,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) 2717,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) 910,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -910,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -2717,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -4488,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -6201,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -7832,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -9359,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -10764,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -12028,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -13133,
        (short) -7680,
        (short) 0,
        (short) 0
      },
      {
        (short) -13920,
        (short) -7522,
        (short) 0,
        (short) 0
      },
      {
        (short) -14507,
        (short) -7366,
        (short) 0,
        (short) 0
      },
      {
        (short) -14731,
        (short) -7059,
        (short) 0,
        (short) 0
      },
      {
        (short) 5376,
        (short) -9216,
        (short) 3328,
        (short) -3072
      },
      {
        (short) -6400,
        (short) -7168,
        (short) -3328,
        (short) -2304
      },
      {
        (short) -10496,
        (short) -7424,
        (short) -3584,
        (short) -1024
      },
      {
        (short) -167,
        (short) -2722,
        (short) -494,
        (short) -541
      },
      {
        (short) -7430,
        (short) -2221,
        (short) -2298,
        (short) 424
      },
      {
        (short) -8001,
        (short) -3166,
        (short) -2814,
        (short) 289
      },
      {
        (short) 6018,
        (short) -4750,
        (short) 2649,
        (short) -1298
      },
      {
        (short) 3798,
        (short) -6946,
        (short) 3875,
        (short) -1216
      },
      {
        (short) -8237,
        (short) -2596,
        (short) -2071,
        (short) 227
      },
      {
        (short) 9199,
        (short) 1982,
        (short) -1382,
        (short) -2316
      },
      {
        (short) 13021,
        (short) -3044,
        (short) -3792,
        (short) 1267
      },
      {
        (short) 13112,
        (short) -4487,
        (short) -2250,
        (short) 1665
      },
      {
        (short) -1668,
        (short) -3744,
        (short) -6456,
        (short) 840
      },
      {
        (short) 7819,
        (short) -4328,
        (short) 2111,
        (short) -506
      },
      {
        (short) 9571,
        (short) -1336,
        (short) -757,
        (short) 487
      },
      {
        (short) 10032,
        (short) -2562,
        (short) 300,
        (short) 199
      },
      {
        (short) -4745,
        (short) -4122,
        (short) -5486,
        (short) -1493
      },
      {
        (short) -5896,
        (short) 2378,
        (short) -4787,
        (short) -6947
      },
      {
        (short) -1193,
        (short) -9117,
        (short) -1237,
        (short) -3114
      },
      {
        (short) 2783,
        (short) -7108,
        (short) -1575,
        (short) -1447
      },
      {
        (short) -7334,
        (short) -2062,
        (short) -2212,
        (short) 446
      },
      {
        (short) 6127,
        (short) -2577,
        (short) -315,
        (short) -18
      },
      {
        (short) 9457,
        (short) -1858,
        (short) 102,
        (short) 258
      },
      {
        (short) 7876,
        (short) -4483,
        (short) 2126,
        (short) -538
      },
      {
        (short) -7172,
        (short) -1795,
        (short) -2069,
        (short) 482
      },
      {
        (short) -7358,
        (short) -2102,
        (short) -2233,
        (short) 440
      },
      {
        (short) -9170,
        (short) -3509,
        (short) -2674,
        (short) -391
      },
      {
        (short) -2638,
        (short) -2647,
        (short) -1929,
        (short) -1637
      },
      {
        (short) 1873,
        (short) 9183,
        (short) 1860,
        (short) -5746
      },
      {
        (short) 9214,
        (short) 1859,
        (short) -1124,
        (short) -2427
      },
      {
        (short) 13204,
        (short) -3012,
        (short) -4139,
        (short) 1370
      },
      {
        (short) 12437,
        (short) -4792,
        (short) -256,
        (short) 622
      },
      {
        (short) -2653,
        (short) -1144,
        (short) -3182,
        (short) -6878
      },
      {
        (short) 9331,
        (short) -1048,
        (short) -828,
        (short) 507
      },
      {
        (short) 1642,
        (short) -620,
        (short) -946,
        (short) -4229
      },
      {
        (short) 4246,
        (short) -7585,
        (short) -533,
        (short) -2259
      },
      {
        (short) -8988,
        (short) -3891,
        (short) -2807,
        (short) 44
      },
      {
        (short) -2562,
        (short) -2735,
        (short) -1730,
        (short) -1899
      },
      {
        (short) 3182,
        (short) -483,
        (short) -714,
        (short) -1421
      },
      {
        (short) 7937,
        (short) -3844,
        (short) 2821,
        (short) -1019
      },
      {
        (short) 10069,
        (short) -2609,
        (short) 314,
        (short) 195
      },
      {
        (short) 8400,
        (short) -3297,
        (short) 1551,
        (short) -155
      },
      {
        (short) -8529,
        (short) -2775,
        (short) -2432,
        (short) -336
      },
      {
        (short) 9477,
        (short) -1882,
        (short) 108,
        (short) 256
      },
      {
        (short) 75,
        (short) -2241,
        (short) -298,
        (short) -6937
      },
      {
        (short) -9143,
        (short) -4160,
        (short) -2963,
        (short) 5
      },
      {
        (short) -7270,
        (short) -1958,
        (short) -2156,
        (short) 460
      },
      {
        (short) -2740,
        (short) 3745,
        (short) 5936,
        (short) -1089
      },
      {
        (short) 8993,
        (short) 1948,
        (short) -683,
        (short) -2704
      },
      {
        (short) 13101,
        (short) -2835,
        (short) -3854,
        (short) 1055
      },
      {
        (short) 9543,
        (short) -1961,
        (short) 130,
        (short) 250
      },
      {
        (short) 5272,
        (short) -4270,
        (short) 3124,
        (short) -3157
      },
      {
        (short) -7696,
        (short) -3383,
        (short) -2907,
        (short) -456
      },
      {
        (short) 7309,
        (short) 2523,
        (short) 434,
        (short) -2461
      },
      {
        (short) 10275,
        (short) -2867,
        (short) 391,
        (short) 172
      },
      {
        (short) 10940,
        (short) -3721,
        (short) 665,
        (short) 97
      },
      {
        (short) 24,
        (short) -310,
        (short) -1262,
        (short) 320
      },
      {
        (short) -8122,
        (short) -2411,
        (short) -2311,
        (short) -271
      },
      {
        (short) -8511,
        (short) -3067,
        (short) -2337,
        (short) 163
      },
      {
        (short) 326,
        (short) -3846,
        (short) 419,
        (short) -933
      },
      {
        (short) 8895,
        (short) 2194,
        (short) -541,
        (short) -2880
      },
      {
        (short) 12073,
        (short) -1876,
        (short) -2017,
        (short) -601
      },
      {
        (short) 8729,
        (short) -3423,
        (short) 1674,
        (short) -169
      },
      {
        (short) 12950,
        (short) -3847,
        (short) -3007,
        (short) 1946
      },
      {
        (short) 10038,
        (short) -2570,
        (short) 302,
        (short) 198
      },
      {
        (short) 9385,
        (short) -2757,
        (short) 1008,
        (short) 41
      },
      {
        (short) -4720,
        (short) -5006,
        (short) -2852,
        (short) -1161
      },
      {
        (short) 7869,
        (short) -4326,
        (short) 2135,
        (short) -501
      },
      {
        (short) 2450,
        (short) -8597,
        (short) 1299,
        (short) -2780
      },
      {
        (short) 10192,
        (short) -2763,
        (short) 360,
        (short) 181
      },
      {
        (short) 11313,
        (short) -4213,
        (short) 833,
        (short) 53
      },
      {
        (short) 10154,
        (short) -2716,
        (short) 345,
        (short) 185
      },
      {
        (short) 9638,
        (short) -1417,
        (short) -737,
        (short) 482
      },
      {
        (short) 3854,
        (short) -4554,
        (short) 2843,
        (short) -3397
      },
      {
        (short) 6699,
        (short) -5659,
        (short) 2249,
        (short) -1074
      },
      {
        (short) 11082,
        (short) -3908,
        (short) 728,
        (short) 80
      },
      {
        (short) -1026,
        (short) -9810,
        (short) -805,
        (short) -3462
      },
      {
        (short) 10396,
        (short) -3746,
        (short) 1367,
        (short) -96
      },
      {
        (short) 10287,
        (short) 988,
        (short) -1915,
        (short) -1437
      },
      {
        (short) 7953,
        (short) 3878,
        (short) -764,
        (short) -3263
      },
      {
        (short) 12689,
        (short) -3375,
        (short) -3354,
        (short) 2079
      },
      {
        (short) 6641,
        (short) 3166,
        (short) 231,
        (short) -2089
      },
      {
        (short) -2348,
        (short) -7354,
        (short) -1944,
        (short) -4122
      },
      {
        (short) 9290,
        (short) -4039,
        (short) 1885,
        (short) -246
      },
      {
        (short) 4633,
        (short) -6403,
        (short) 1748,
        (short) -1619
      },
      {
        (short) 11247,
        (short) -4125,
        (short) 802,
        (short) 61
      },
      {
        (short) 9807,
        (short) -2284,
        (short) 219,
        (short) 222
      },
      {
        (short) 9736,
        (short) -1536,
        (short) -706,
        (short) 473
      },
      {
        (short) 8440,
        (short) -3436,
        (short) 1562,
        (short) -176
      },
      {
        (short) 9307,
        (short) -1021,
        (short) -835,
        (short) 509
      },
      {
        (short) 1698,
        (short) -9025,
        (short) 688,
        (short) -3037
      },
      {
        (short) 10214,
        (short) -2791,
        (short) 368,
        (short) 179
      },
      {
        (short) 8390,
        (short) 3248,
        (short) -758,
        (short) -2989
      },
      {
        (short) 7201,
        (short) 3316,
        (short) 46,
        (short) -2614
      },
      {
        (short) -88,
        (short) -7809,
        (short) -538,
        (short) -4571
      },
      {
        (short) 6193,
        (short) -5189,
        (short) 2760,
        (short) -1245
      },
      {
        (short) 12325,
        (short) -1290,
        (short) -3284,
        (short) 253
      },
      {
        (short) 13064,
        (short) -4075,
        (short) -2824,
        (short) 1877
      },
      {
        (short) 5333,
        (short) 2999,
        (short) 775,
        (short) -1132
      }
    };
    public string file = "";
    public VAG.VAGFile Data;

    public VAG()
    {
      this.Data = new VAG.VAGFile();
      this.file = "";
    }

    public VAG(string filepath)
    {
      this.Data = new VAG.VAGFile();
      this.file = filepath;
    }

    public void VAGReader(bool SaveToDisk)
    {
      if (!File.Exists(this.file + ".vag"))
        return;
      this.Data = new VAG.VAGFile()
      {
        Blocks = new List<VAG.VAGBlocks>()
      };
      KKtLib.IO io1 = KKtLib.IO.OpenReader(this.file + ".vag");
      if (io1.ReadString(4L) == "VAGp")
      {
        VAG.VAGBlocks vagBlocks = new VAG.VAGBlocks();
        double[,] numArray1 = new double[0, 0];
        byte num1 = 0;
        this.Data.Version = io1.ReadInt32Endian(true);
        int num2 = (int) io1.ReadUInt32();
        this.Data.Size = io1.ReadInt32Endian(true);
        this.Data.SampleRate = io1.ReadInt32Endian(true);
        int num3 = (int) io1.ReadUInt32();
        int num4 = (int) io1.ReadUInt32();
        int num5 = (int) io1.ReadUInt16();
        this.Data.Channels = io1.ReadInt16();
        if (this.Data.Channels < (short) 2)
          this.Data.Channels = (short) 1;
        double[,] numArray2 = new double[5, (int) this.Data.Channels];
        this.Data.Name = io1.ReadString(16L);
        KKtLib.IO io2 = new KKtLib.IO((Stream) null, false);
        if (SaveToDisk)
          io2 = KKtLib.IO.OpenWriter(this.file + ".pcm", true);
        while (io1.Position < this.Data.Size + 48)
        {
          vagBlocks.Flag = (HalfByte) 0;
          vagBlocks = new VAG.VAGBlocks()
          {
            Block = new VAG.VAGBlock[(int) this.Data.Channels]
          };
          for (short index1 = 0; (int) index1 < (int) this.Data.Channels; ++index1)
          {
            vagBlocks.Block[(int) index1] = new VAG.VAGBlock()
            {
              Data = new double[28],
              Offset = io1.Position
            };
            vagBlocks.Block[(int) index1].PredictNR = (byte) io1.ReadHalfByte();
            vagBlocks.Block[(int) index1].ShiftFactor = io1.ReadHalfByte();
            vagBlocks.Block[(int) index1].PredictNR |= (byte) ((uint) (byte) io1.ReadHalfByte() << 4);
            vagBlocks.Flag = io1.ReadHalfByte();
            for (byte index2 = 0; index2 < (byte) 28; ++index2)
            {
              vagBlocks.Block[(int) index1].Data[(int) index2] = (numArray2[1, (int) index1] * (double) VAG.HEVAG_coefs[(int) vagBlocks.Block[(int) index1].PredictNR, 0] + numArray2[2, (int) index1] * (double) VAG.HEVAG_coefs[(int) vagBlocks.Block[(int) index1].PredictNR, 1] + numArray2[3, (int) index1] * (double) VAG.HEVAG_coefs[(int) vagBlocks.Block[(int) index1].PredictNR, 2] + numArray2[4, (int) index1] * (double) VAG.HEVAG_coefs[(int) vagBlocks.Block[(int) index1].PredictNR, 3]) * 1024.0;
              if (((int) index2 & 1) == 0)
                num1 = io1.ReadByte();
              else
                num1 >>= 4;
              numArray2[0, (int) index1] = (double) ((int) num1 & 15);
              if (numArray2[0, (int) index1] > 7.0)
                numArray2[0, (int) index1] -= 16.0;
              numArray2[0, (int) index1] = (double) ((long) numArray2[0, (int) index1] << 20 - (int) (byte) vagBlocks.Block[(int) index1].ShiftFactor);
              vagBlocks.Block[(int) index1].Data[(int) index2] += numArray2[0, (int) index1];
              if (vagBlocks.Block[(int) index1].Data[(int) index2] != 0.0)
                vagBlocks.Block[(int) index1].Data[(int) index2] += 128.0;
              vagBlocks.Block[(int) index1].Data[(int) index2] /= 8388608.0;
              numArray2[4, (int) index1] = numArray2[3, (int) index1];
              numArray2[3, (int) index1] = numArray2[2, (int) index1];
              numArray2[2, (int) index1] = numArray2[1, (int) index1];
              numArray2[1, (int) index1] = vagBlocks.Block[(int) index1].Data[(int) index2];
            }
          }
          if (SaveToDisk)
          {
            for (byte index1 = 0; index1 < (byte) 28; ++index1)
            {
              for (short index2 = 0; (int) index2 < (int) this.Data.Channels; ++index2)
                io2.Write(vagBlocks.Block[(int) index2].Data[(int) index1], (short) 4, (ushort) 3);
            }
          }
          this.Data.Blocks.Add(vagBlocks);
        }
        io2.Close();
        if (SaveToDisk)
        {
          KKtLib.IO io3 = KKtLib.IO.OpenWriter(this.file + ".vagb", true);
          io3.Write("VAGb");
          io3.WriteEndian(this.Data.Version, true);
          io3.Write(0);
          io3.WriteEndian(this.Data.Blocks.Count / 2 + this.Data.Blocks.Count % 2, true);
          io3.WriteEndian(this.Data.SampleRate, true);
          io3.Write(0);
          io3.Write(0);
          io3.Write((ushort) 0);
          io3.Write((ushort) this.Data.Channels);
          io3.Write(0);
          io3.Write(0);
          io3.Write(0);
          io3.Write(0);
          for (int index = 0; index < this.Data.Blocks.Count; ++index)
            io3.Write(this.Data.Blocks[index].Flag);
          io3.Close();
        }
      }
      io1.Close();
    }

    public void WAVWriter()
    {
      Main.WAVHeader Header1 = new Main.WAVHeader();
      KKtLib.IO io = KKtLib.IO.OpenWriter(this.file + ".0.wav", true);
      io.Seek(44L, SeekOrigin.Begin);
      int index1 = 0;
      int num = 0;
      Main.WAVHeader wavHeader;
      for (; index1 < this.Data.Blocks.Count && ((int) this.Data.Blocks[index1].Flag <= 4 || (int) this.Data.Blocks[index1].Flag == 6); ++index1)
      {
        if ((int) this.Data.Blocks[index1].Flag == 3 || (int) this.Data.Blocks[index1].Flag == 6)
        {
          wavHeader = new Main.WAVHeader();
          wavHeader.Bytes = (short) 4;
          wavHeader.Channels = (int) this.Data.Channels;
          wavHeader.Format = (ushort) 3;
          wavHeader.SampleRate = this.Data.SampleRate;
          wavHeader.Size = io.Position - 44;
          Header1 = wavHeader;
          io.Write(Header1);
          io.Close();
          ++num;
          io = KKtLib.IO.OpenWriter(this.file + "." + (object) num + ((int) this.Data.Blocks[index1].Flag == 6 ? (object) ".loop" : (object) "") + ".wav", true);
          io.Seek(44L, SeekOrigin.Begin);
        }
        if (index1 <= 0 || (int) this.Data.Blocks[index1 - 1].Flag != 1)
        {
          for (byte index2 = 0; index2 < (byte) 28; ++index2)
          {
            for (short index3 = 0; (int) index3 < (int) this.Data.Channels; ++index3)
              io.Write(this.Data.Blocks[index1].Block[(int) index3].Data[(int) index2], Header1.Bytes, Header1.Format);
          }
        }
        else
          break;
      }
      wavHeader = new Main.WAVHeader();
      wavHeader.Bytes = (short) 4;
      wavHeader.Channels = (int) this.Data.Channels;
      wavHeader.Format = (ushort) 3;
      wavHeader.SampleRate = this.Data.SampleRate;
      wavHeader.Size = io.Position - 44;
      Main.WAVHeader Header2 = wavHeader;
      io.Write(Header2);
      io.Close();
    }

    public void WAVReader()
    {
    }

    public void VAGWriter()
    {
      char[] chArray1 = new char[128];
      double[] d_samples = new double[28];
      byte[] four_bit = new byte[28];
      byte predict_nr = 0;
      byte shift_factor = 0;
      char[] chArray2 = new char[4];
      double[] samples = new double[28];
      KKtLib.IO io1 = KKtLib.IO.OpenReader(this.file + ".wav");
      Main.WAVHeader wavHeader = io1.ReadWAVHeader();
      if (wavHeader.IsSupported)
      {
        KKtLib.IO io2 = KKtLib.IO.OpenWriter(this.file + ".vag", true);
        this.Data = new VAG.VAGFile()
        {
          Blocks = new List<VAG.VAGBlocks>()
        };
        VAG.VAGBlock[] vagBlockArray1 = new VAG.VAGBlock[0];
        while (io1.Position < wavHeader.Size + wavHeader.HeaderSize)
        {
          VAG.VAGBlock[] vagBlockArray2 = new VAG.VAGBlock[wavHeader.Channels];
          for (byte index1 = 0; index1 < (byte) 28; ++index1)
          {
            for (short index2 = 0; (int) index2 < wavHeader.Channels; ++index2)
              vagBlockArray2[(int) index2].Data[(int) index1] = io1.ReadWAVSample(wavHeader.Bytes, wavHeader.Format);
          }
          this.Data.Blocks.Add(new VAG.VAGBlocks()
          {
            Block = vagBlockArray2
          });
        }
        int val1 = 0;
        int num1 = wavHeader.Size / wavHeader.Channels;
        io1.Seek((long) wavHeader.HeaderSize, SeekOrigin.Begin);
        while (num1 > 0)
        {
          int num2 = num1 >= 28 ? 28 : num1;
          for (int index = 0; index < num2 - num2 % 28; ++index)
            samples[index] = io1.ReadWAVSample(wavHeader.Bytes, wavHeader.Format);
          if (num2 % 28 != 0)
          {
            for (int index = num2 % 28; index < 28; ++index)
              samples[index] = 0.0;
          }
          this.FindPredict(samples, ref d_samples, ref four_bit, ref predict_nr, ref shift_factor);
          int val2 = (int) (byte) ((uint) predict_nr << 4 | (uint) shift_factor);
          io2.Write(val2);
          io2.Write(val1);
          for (int index = 0; index < 28; index += 2)
            io2.Write((byte) (((int) four_bit[index + 1] & 15) << 4 | (int) four_bit[index] & 15));
          num1 -= 28;
          if (num1 < 28)
            val1 = 1;
        }
        io2.Close();
      }
      io1.Close();
    }

    private void FindPredict(
      double[] samples,
      ref double[] d_samples,
      ref byte[] four_bit,
      ref byte predict_nr,
      ref byte shift_factor)
    {
      double[,] numArray1 = new double[28, 5];
      double num1 = 10000000000.0;
      double[] numArray2 = new double[5];
      double num2 = 0.0;
      double num3 = 0.0;
      double num4 = 0.0;
      double num5 = 0.0;
      double num6 = 0.0;
      for (byte index1 = 0; index1 < (byte) 5; ++index1)
      {
        numArray2[(int) index1] = 0.0;
        num5 = num2;
        num6 = num3;
        for (int index2 = 0; index2 < 28; ++index2)
        {
          double num7 = samples[index2];
          if (num7 > 30719.0)
            num7 = 30719.0;
          if (num7 < -30720.0)
            num7 = -30720.0;
          double num8 = num7 + num5 * (double) VAG.HEVAG_coefs[(int) index1, 0] / 8192.0 + num6 * (double) VAG.HEVAG_coefs[(int) index1, 1] / 8192.0;
          numArray1[index2, (int) index1] = num8;
          if (Math.Abs(num8) > numArray2[(int) index1])
            numArray2[(int) index1] = Math.Abs(num8);
          num6 = num5;
          num5 = num7;
        }
        if (numArray2[(int) index1] < num1)
        {
          num1 = numArray2[(int) index1];
          predict_nr = index1;
        }
        if (num1 <= 7.0)
        {
          predict_nr = (byte) 0;
          break;
        }
      }
      for (byte index = 0; index < (byte) 28; ++index)
        d_samples[(int) index] = numArray1[(int) index, (int) predict_nr];
      int num9 = (int) num1;
      int num10 = 16384;
      shift_factor = (byte) 0;
      for (; shift_factor < (byte) 12 && (num10 & num9 + (num10 >> 3)) != 16384; num10 >>= 1)
        ++shift_factor;
      num4 = 0.0;
      double num11 = 0.0;
      double num12 = 0.0;
      for (byte index = 0; index < (byte) 28; ++index)
      {
        double num7 = d_samples[(int) index] + num11 * (double) VAG.HEVAG_coefs[(int) predict_nr, 0] / 8192.0 + num12 * (double) VAG.HEVAG_coefs[(int) predict_nr, 1] / 8192.0;
        int num8 = (int) ((long) ((int) (num7 * (double) (1 << (int) shift_factor)) + 2048) & 4294963200L);
        if (num8 > (int) short.MaxValue)
          num8 = (int) short.MaxValue;
        if (num8 < (int) short.MinValue)
          num8 = (int) short.MinValue;
        four_bit[(int) index] = (byte) (num8 >> 12 & 15);
        int num13 = num8 >> (int) shift_factor;
        num12 = num11;
        num11 = (double) num13 - num7;
      }
    }

    public struct VAGFile
    {
      public int Size;
      public int Version;
      public int SampleRate;
      public short Channels;
      public string Name;
      public List<VAG.VAGBlocks> Blocks;
    }

    public struct VAGBlocks
    {
      public HalfByte Flag;
      public VAG.VAGBlock[] Block;
    }

    public struct VAGBlock
    {
      public int Offset;
      public byte PredictNR;
      public HalfByte ShiftFactor;
      public double[] Data;
    }
  }
}
